﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UM_SERVER.ProcessModel;
using UM_SERVER.Net;
using UM_SERVER.Packet.InPacket;
using UM_SERVER.User;

namespace UM_SERVER.ProcessModel
{
    class ProcessLoginModel : Model
    {
        private static String PackageBody;
        private static String Protocol;

        /// <summary>
        ///     提供回复成功登陆信息给指定用户
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="Mession" type="int">
        ///     <para>
        ///         用户权限
        ///     </para>
        /// </param>
        /// <param name="TalkPort" type="int">
        ///     <para>
        ///         聊天端口
        ///     </para>
        /// </param>
        /// <param name="UserIp" type="string">
        ///     <para>
        ///         用户IP地址
        ///     </para>
        /// </param>
        /// <param name="LoginLive" type="UM_SERVER.Packet.InPacket.LoginPacket.LoginState">
        ///     <para>
        ///         登陆状态
        ///     </para>
        /// </param>
        /// <param name="SessionCode">
        ///     <para>
        ///         当前用户基础会话码
        ///     </para>      
        /// </param>
        /// <param name="loginState">
        ///     <para>
        ///         当前用户在线状态
        ///     </para>      
        /// </param>
        public static void ACK_SucessfulMessageToUser(String username, int mession, int TalkPort, String UserIp, String SessionCode, LoginPacket.LoginState loginState)
        {
            Offset = 0;
            byte[] Username = Encoding.Default.GetBytes(username);
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] Mession = BitConverter.GetBytes(mession);
            byte[] LoginState = BitConverter.GetBytes((int)(loginState));
            byte[] Result = BitConverter.GetBytes(true);
            byte[] SessionLength = BitConverter.GetBytes(SessionCode.Length);
            byte[] Session = Encoding.Default.GetBytes(SessionCode);
            byte[] PackageBody = new byte[Username.Length + Session.Length + 17];
            //赋值用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, 4);
            Offset += 4;
            //赋值用户名
            Array.ConstrainedCopy(Username, 0, PackageBody, Offset, Username.Length);
            Offset += Username.Length;
            //赋值登陆结果
            Array.ConstrainedCopy(Result, 0, PackageBody, Offset, 1);
            Offset += 1;
            //赋值权限
            Array.ConstrainedCopy(Mession, 0, PackageBody, Offset, 4);
            Offset += 4;
            //赋值会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, PackageBody, Offset, 4);
            Offset += 4;
            //赋值会话ID
            Array.ConstrainedCopy(Session, 0, PackageBody, Offset, Session.Length);
            Offset += Session.Length;
            //赋值上线状态
            Array.ConstrainedCopy(LoginState, 0, PackageBody, Offset, 4);
            Offset += 4;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyLogin",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_LOGIN_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          PackageBody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, TalkPort, UserIp);
        }

        /// <summary>
        ///     提供回复失败登陆信息给指定用户
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="TalkPort" type="int">
        ///     <para>
        ///         聊天端口
        ///     </para>
        /// </param>
        /// <param name="UserIp" type="string">
        ///     <para>
        ///         用户IP地址
        ///     </para>
        /// </param>
        public static void ACK_FailMessageToUser(String username, int TalkPort, String UserIp)
        {
            Offset = 0;
            byte[] Username = Encoding.Default.GetBytes(username);
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] Mession = BitConverter.GetBytes(-1);
            byte[] LoginState = BitConverter.GetBytes(-1);
            byte[] Result = BitConverter.GetBytes(false);
            byte[] SessionLength = BitConverter.GetBytes(16);
            byte[] Session = Encoding.Default.GetBytes("0000000000000000");
            byte[] PackageBody = new byte[Username.Length + Session.Length + 17];
            //赋值用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, 4);
            Offset += 4;
            //赋值用户名
            Array.ConstrainedCopy(Username, 0, PackageBody, Offset, Username.Length);
            Offset += Username.Length;
            //赋值登陆结果
            Array.ConstrainedCopy(Result, 0, PackageBody, Offset, 1);
            Offset += 1;
            //赋值权限
            Array.ConstrainedCopy(Mession, 0, PackageBody, Offset, 4);
            Offset += 4;
            //赋值会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, PackageBody, Offset, 4);
            Offset += 4;
            //赋值会话ID
            Array.ConstrainedCopy(Session, 0, PackageBody, Offset, Session.Length);
            Offset += Session.Length;
            //赋值上线状态
            Array.ConstrainedCopy(LoginState, 0, PackageBody, Offset, 4);
            Offset += 4;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyLogin",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_LOGIN_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          PackageBody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, TalkPort, UserIp);
        }
    }
}
